//
//  QPointShopVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/3/28.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QPointShopVC.h"
#import "QLoginVC.h"
@interface QPointShopVC ()<UIWebViewDelegate,UIGestureRecognizerDelegate>
{
    UIWebView * _web;
    NSURLRequest *_request;
    NSString * _url;
}
//返回按钮
@property (nonatomic, strong) UIBarButtonItem *backItem;
//关闭按钮
@property (nonatomic, strong) UIBarButtonItem *closeItem;

@end

@implementation QPointShopVC

- (id)init{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self WebView];
    [self requestWebURL];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestWebURL) name:kNotificationLoginSignup object:nil];
    
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    _request = nil;
    _web = nil;
}
- (void)requestWebURL{
    NSString *url = [QParamService urlWithAction:@"integralMallURL"];
    NSDictionary *param = [QParamService integralMallURL];
    [QViewUtility showMBHudToView:self.view hideBg:YES];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            _request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:[basModel.items objectForKey:@"autologinurl"]]];
            [_web loadRequest:_request];

        }else{
            
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
    
}


-(void)WebView
{
    _web=[[UIWebView alloc]initWithFrame:self.view.frame];
    NSString *version = [UIDevice currentDevice].systemVersion;
    if (version.doubleValue >= 11.0) {
        
        _web.frame = CGRectMake(0, kNavHeight+kStatusHeight, kScreenWidth, kScreenHeight-kNavHeight-kStatusHeight);
        
    }else{
        
        _web.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    }
    _request = [[NSURLRequest alloc]init];
    _web.scalesPageToFit=YES;
    _web.delegate=self;
    [self.view addSubview:_web];
    
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSMutableString *url=[[NSMutableString alloc]initWithString:[request.URL absoluteString]];

    NSArray *urlComps = [url componentsSeparatedByString:@"://"];
    if([urlComps count] && [[urlComps objectAtIndex:0] isEqualToString:@"objc"]){
        if([[urlComps objectAtIndex:1] hasPrefix:@"duiba/"]){
            NSArray *d=[[urlComps objectAtIndex:1] componentsSeparatedByString:@"/"];
            if(d.count==2 && [d[1] isEqualToString:@"login"]){
                NSMutableDictionary *dict=[NSMutableDictionary dictionary];
                [dict setObject:webView.request.URL.absoluteString forKey:@"currentUrl"];
                [dict setObject:webView forKey:@"webView"];

                QLoginVC *vc = [[QLoginVC alloc] init];
                [self.navigationController pushViewController:vc animated:YES];

                return NO;
            }
        }
    }


    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    //[QViewUtility showMBHudToView:self.view hideBg:YES];

    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [QViewUtility hideMBHudToView:self.view];
    
    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    
    if ([_web canGoBack])
    {
        self.navigationItem.leftBarButtonItems = @[self.backItem, self.closeItem];
    }
    else
    {
        self.navigationItem.leftBarButtonItems = @[self.backItem];
        
    }

}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    [QViewUtility hideMBHudToView:self.view];

    [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:@"页面加载失败" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
}

- (UIBarButtonItem *)backItem
{
    if (!_backItem) {
        _backItem = [[UIBarButtonItem alloc] init];
        
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        //15 2 23 23
        btn.frame=CGRectMake(5, 2, 60, 23);
        [btn setTitle:@"返回  " forState:0];
        [btn setImage:[UIImage imageNamed:@"nav_whiteBack"] forState:0];
        [btn addTarget:self action:@selector(backNative) forControlEvents:UIControlEventTouchUpInside];
        
        _backItem.customView = btn;
    }
    return _backItem;
}

- (UIBarButtonItem *)closeItem
{
    if (!_closeItem) {
        _closeItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(closeNative)];
        [_closeItem setTintColor:[UIColor whiteColor]];
    }
    return _closeItem;
}


- (void)backNative
{
    if ([_web canGoBack]) {
        [_web goBack];
    } else {
        [self closeNative];
    }
}

-(void)closeNative
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
